package com.aos.eap.activity;

import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.adapter.CapHistoryListAdapter;
import com.aos.eap.base.App;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.CapBean;
import com.aos.eap.bean.RepairBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 更换劈刀
 */
public class EE_ChangeCleaverActivity extends BaseDialogActivity {

    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etCleaverLotId)
    FreeUI_ClearEditText etCleaverLotId;
    @BindView(R.id.etCleaverLiao)
    FreeUI_ClearEditText etCleaverLiao;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.ivEqpTypeArrow)
    ImageView ivEqpTypeArrow;
    @BindView(R.id.flEqpType)
    FrameLayout flEqpType;
    @BindView(R.id.rbYes)
    RadioButton rbYes;
    @BindView(R.id.rbNo)
    RadioButton rbNo;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvLuoValue)
    TextView tvLuoValue;
    @BindView(R.id.tvSurplusOld)
    TextView tvSurplusOld;
    @BindView(R.id.tvUseCount)
    TextView tvUseCount;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.rvAlarmList)
    RecyclerView rvAlarmList;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.tvChange)
    TextView tvChange;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private RepairBean repairBean;
    private CapBean capBean;
    private FreeUI_GeneralPop reasonPop;
    private CapHistoryListAdapter capHistoryListAdapter;
    private String isOk = "N";
    private FreeUI_GeneralFragmentDialog tipDialog;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ee_change_cleaver;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        repairBean = (RepairBean) getIntent().getSerializableExtra("repairBean");
        tvEqpId.setText(repairBean.getEqpId());
        tvLuoValue.setText(repairBean.getUsgLow() + "/" + repairBean.getUsgHigh());
        tvLotId.setText(repairBean.getLotId());
        tvSurplusOld.setText(repairBean.getScrewCnt());
        tvUseCount.setText(repairBean.getCapCnt());
        requestInfo();

        rvAlarmList.setLayoutManager(new LinearLayoutManager(EE_ChangeCleaverActivity.this));
        rvAlarmList.setItemAnimator(new DefaultItemAnimator());
        rvAlarmList.addItemDecoration(new DividerItemDecoration(EE_ChangeCleaverActivity.this, 1));
        capHistoryListAdapter = new CapHistoryListAdapter(null);
        capHistoryListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        capHistoryListAdapter.isFirstOnly(true);
        rvAlarmList.setAdapter(capHistoryListAdapter);
    }

    private void requestInfo() {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", repairBean.getEqpId());
        NetUtils.requestGetNet(this, "EMS_AddCapLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<CapBean>>(
                new TypeToken<BaseResponseBean<CapBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<CapBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    capBean = response.body().getResponse().getBody();
                    List<String> reasonList = new ArrayList<>();
                    for (CapBean.ReasonBean entity : capBean.getReasonList()) {
                        reasonList.add(entity.getReason());
                    }
                    reasonPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEqpType.getWidth(),
                            reasonList, ivEqpTypeArrow, (s, i) -> {
                                tvEqpType.setText(s);
                            });
                    capHistoryListAdapter.setNewData(capBean.getCapHisList());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<CapBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_AddCapLoad");
            }
        });
    }

    @Override
    protected void initData() {
        MyUtils.setEditTextListener(etCleaverLiao, this, s -> {
            MyUtils.setEditTextFocus(etCleaverLotId);
            etCleaverLotId.setText("");
        });
        MyUtils.setEditTextListener(etCleaverLotId, this, s -> requestCheck());
    }

    private void requestCheck() {
        String cleaverLotId = etCleaverLotId.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(cleaverLotId)) {
            showToast("请扫描新劈刀批次号", false);
            MyUtils.setEditTextFocus(etCleaverLotId);
            return;
        }
        String liao = etCleaverLiao.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(liao)) {
            showToast("请扫描新劈刀料号", false);
            MyUtils.setEditTextFocus(etCleaverLiao);
            return;
        }
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", repairBean.getEqpId());
        map.addProperty("CapLotId", cleaverLotId);
        map.addProperty("CapMatId", liao);
        NetUtils.requestGetNet(this, "EMS_CapCheck", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().getResponse().getReturn().getReturnCode().toUpperCase().equals("SUCCESS")) {
                    showToast("比对成功", true);
                } else {
                    MyUtils.setEditTextFocus(etCleaverLiao);
                    etCleaverLiao.setText("");
                    etCleaverLotId.setText("");
                    tipDialog = FreeApi_DialogUtils.getConfirmDialog(App.app, "确定",
                            response.body().getResponse().getReturn().getReturnMessage(), () -> {
                            });
                    tipDialog.show(getSupportFragmentManager(), "DIALOG");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_CapCheck");
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("上劈刀");
        freeUI_titleView.setLeftListener(view -> showBackDialog(EE_ChangeCleaverActivity.this));
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.flEqpType, R.id.rbYes, R.id.rbNo, R.id.tvReplay, R.id.tvChange})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.flEqpType:
                if (reasonPop != null) {
                    if (reasonPop.isShowing()) {
                        reasonPop.dismiss();
                    } else {
                        FreeApi_PopUtils.setArrowShow(ivEqpTypeArrow);
                        reasonPop.showAsDropDown(flEqpType, 0, 0);
                    }
                }
                break;
            case R.id.rbYes:
                isOk = "Y";
                break;
            case R.id.rbNo:
                isOk = "N";
                break;
            case R.id.tvReplay:
                JsonObject map1 = new JsonObject();
                map1.addProperty("EqpID", repairBean.getEqpId());
                NetUtils.requestGetNet(this, "EMS_CapReDo", map1, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                        new TypeToken<BaseResponseBean<?>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<?>> response) {
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            showToast("提交成功", true);
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<?>> response) {
                        super.onError(response);
                        uploadLog(response.getException());
                        loadError(response.getException(), "EMS_CapReDo");
                    }
                });
                break;
            case R.id.tvChange:
                if (capBean == null) {
                    showToast("未获取到劈刀信息，请重新进入", false);
                    return;
                }
                String cleaverLotId = etCleaverLotId.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(cleaverLotId)) {
                    showToast("请扫描新劈刀批次号", false);
                    MyUtils.setEditTextFocus(etCleaverLotId);
                    return;
                }
                String liao = etCleaverLiao.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(liao)) {
                    showToast("请扫描新劈刀料号", false);
                    MyUtils.setEditTextFocus(etCleaverLiao);

                    return;
                }
                String reason = tvEqpType.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(reason)) {
                    showToast("请选择原因", false);
                    return;
                }
                JsonObject map = new JsonObject();
                map.addProperty("EqpID", repairBean.getEqpId());
                map.addProperty("CapLotId", cleaverLotId);
                map.addProperty("CapMatId", liao);
                map.addProperty("UsgHigh", repairBean.getUsgHigh());
                map.addProperty("UsgLow", repairBean.getUsgLow());
                map.addProperty("Reason", reason);
                map.addProperty("ScrewCnt", Float.parseFloat(repairBean.getScrewCnt()));
                map.addProperty("CapCnt", Float.parseFloat(repairBean.getCapCnt()));
                map.addProperty("IsBurnBall", isOk);
                map.addProperty("Remark", etRemark.getText().toString().trim());
                requestSubmit(map);
                break;
        }
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_AddCap", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(() -> {
                        setResult(2101);
                        EE_ChangeCleaverActivity.this.finish();
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_AddCap");
            }
        });
    }
}
